package com.example.note;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.SearchView;

public class MainActivity extends Activity implements OnQueryTextListener{
	ImageButton btnAdd;
	Button btnRefresh;

	public void getControl() {
		btnAdd = (ImageButton) findViewById(R.id.btnAdd);
		// btnRefresh = (Button) findViewById(R.id.action_refresh);
	}

	public void setEvent() {
		btnAdd.setOnClickListener(new MyProcess());
		// btnRefresh.setOnClickListener(new MyProcess());
	}

	public void InsertNote() {
		Intent i = new Intent(MainActivity.this, Insert_note.class);
		startActivity(i);
	}

	public void RefreshNote() {
		Intent i = new Intent(MainActivity.this, Insert_note.class);
		startActivity(i);
	}

	private class MyProcess implements OnClickListener {

		// create tables
		@Override
		public void onClick(View v) {

			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnAdd:
				InsertNote();
				break;
			
			}
		}
	}
	DatabaseHanler db;
	MyAdapter adapter;
	ListView lstView;
	private SearchView mSearchView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		getControl();
		setEvent();

		 db = new DatabaseHanler(this);
		 //Notes n = new Notes("iii3", "Mãi Mãi Đi Qua", "7:12");
		// db.addNote(n);
		
		 adapter = new MyAdapter(this, db.getNotes());
		 lstView = (ListView) findViewById(R.id.lstView);
		lstView.setAdapter(adapter);
		// db.close();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		MenuItem itemSearch = menu.findItem(R.id.action_search);
		mSearchView = (SearchView) itemSearch.getActionView();
		mSearchView.setOnQueryTextListener(this);
        return true;
		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onQueryTextSubmit(String arg0) {
		if (TextUtils.isEmpty(arg0)) {
			adapter.getFilter().filter("");
			lstView.clearTextFilter();
		} else {
			adapter.getFilter().filter(arg0.toString());
		}
		return false;
	}

	@Override
	public boolean onQueryTextChange(String arg0) {
		// TODO Auto-generated method stub
		return false;
	}
}
